
package com.thomas.sharing.mybatis.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.thomas.sharing.mybatis.model.Notification;

/**
 * @author Thomas D T Chen
 */
@Component
public class CarDao {

	private final SqlSession sqlSession;
	
	@Value("${schema}")
	private String schema;

	public CarDao(SqlSession sqlSession) {
		this.sqlSession = sqlSession;
	}

	public void createTable(String tableName) {
		Map<String, Object> parameterMap = new HashMap<String, Object>();
		parameterMap.put("SCHEMA", schema);
		parameterMap.put("NEW_TABLE_NAME", tableName);
		this.sqlSession.update("car.createNotificationTempTable", parameterMap);
	}

	public void insertTable(String tableName, String sqlId) {
		Map<String, Object> parameterMap = new HashMap<String, Object>();
		parameterMap.put("SCHEMA", schema);
		parameterMap.put("TABLE_NAME", tableName);
		Notification notification1 = new Notification("THOMAS1", "13688886666", 
				"13688886666@mobile.com", "2018 GLA 200 时尚", new Date());
		Notification notification2 = new Notification("THOMAS2", "13688886662", 
				"13688886662@mobile.com", "2018 GLA 200 时尚2", new Date());
		List<Notification> notificationList = new ArrayList<Notification>();
		notificationList.add(notification1);
		notificationList.add(notification2);
		parameterMap.put("notificationList", notificationList);
		this.sqlSession.update(sqlId, parameterMap);
	}
	
	public void updateTable(String tableName, String sqlId) {
		Map<String, Object> parameterMap = new HashMap<String, Object>();
		parameterMap.put("SCHEMA", schema);
		parameterMap.put("TABLE_NAME", tableName);
		parameterMap.put("PHONE", "13688886666");
		this.sqlSession.update(sqlId, parameterMap);
	}
	
	public void callProcedure(String sqlId){
		Map<String, Object> parameterMap = new HashMap<String, Object>();
		parameterMap.put("SCHEMA", schema);
		parameterMap.put("PROCEDURE_NAME", "informCustomerToBack");
		parameterMap.put("_BRANDNAME", "TOYOTA");
		parameterMap.put("_START_DATE", new Date());
		this.sqlSession.update(sqlId, parameterMap);
	}
}